package com.knoya.chejian.pda.module.zupan.pan

import android.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.knoya.chejian.pda.R
import com.knoya.chejian.pda.activity.BaseActivity
import com.knoya.chejian.pda.minterface.OnUserDataInterface
import com.knoya.chejian.pda.module.zupan.ZupanM
import com.knoya.chejian.pda.util.JsonUtil
import com.knoya.chejian.pda.util.Myutil
import kotlinx.android.synthetic.main.activity_yi_pan.*
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONException
import org.json.JSONObject

class YiPanActivity : BaseActivity(), View.OnClickListener {
    lateinit var M :ZupanM
    var pallet_num = ""
    lateinit var dialog: AlertDialog
    override fun getLayoutId(): Int {
         return R.layout.activity_yi_pan
    }

    override fun initCreate() {
        pallet_num = intent.getStringExtra("pallet_num")
        Log.d("TAAAG",pallet_num)
        M = ZupanM()
        iv_header_left.visibility = View.VISIBLE
        tv_header_title.text = "移盘指令"
        Bt_RuZupan.setOnClickListener(this)
        Bt_RuLiku.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            Bt_RuLiku -> {
                dialog = Myutil.showupdatedialog(this@YiPanActivity, "正在上传数据", false)
                M.yipan(this,pallet_num,"1","0",object : OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        Log.d("TAAG",data)
                        var status = ""
                        var msg = ""
                        try {
                            var jo = JSONObject(data)
                            status = JsonUtil.getvolue(jo,"status")
                            msg = JsonUtil.getvolue(jo ,"msg")
                        }catch (e : JSONException){
                            e.printStackTrace()
                        }
                        if(msg != ""){
                            Myutil.tishidialog(this@YiPanActivity,msg)
                        }
                        dialog.dismiss()
                        if(status != "success"){
                            return
                        }
                    }

                    override fun onNot(throwable: String?) {
                        dialog.dismiss()
                    }

                })
            }
            Bt_RuZupan -> {
                dialog = Myutil.showupdatedialog(this@YiPanActivity, "正在上传数据", false)
                M.yipan(this,pallet_num,"0","1",object : OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        Log.d("TAAG",data)
                        var status = ""
                        var msg = ""
                        try {
                            var jo = JSONObject(data)
                            status = JsonUtil.getvolue(jo,"status")
                            msg = JsonUtil.getvolue(jo ,"msg")
                        }catch (e : JSONException){
                            e.printStackTrace()
                        }
                        if(msg != ""){
                            Myutil.tishidialog(this@YiPanActivity,msg)
                        }
                        dialog.dismiss()
                        if(status != "success"){
                            return
                        }
                    }

                    override fun onNot(throwable: String?) {
                        dialog.dismiss()
                    }

                })
            }
        }
    }

}